//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdlib.h>

#include "rutines.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
const char *color_purple  = "\033[00;35m";
const char *color_red     = "\033[01;31m";
const char *color_end     = "\033[00m\n";
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void error (const char* message) {
  
  const char* initBracket = " (";
  const char* endBracket  = ").";
  
  write(STDERR_FILENO, color_red, strlen(color_red));
  write(STDERR_FILENO, message, strlen(message));
  write(STDERR_FILENO, initBracket, strlen(initBracket));
  write(STDERR_FILENO, strerror(errno), strlen(strerror(errno)));
  write(STDERR_FILENO, endBracket, strlen(endBracket));
  write(STDERR_FILENO, color_end, strlen(color_end));

  exit(EXIT_FAILURE);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void error_ (char* message) {

  write(STDERR_FILENO, color_purple, strlen(color_purple));
  write(STDERR_FILENO, message, strlen(message));
  write(STDERR_FILENO, color_end, strlen(color_end));
  
  exit(EXIT_FAILURE);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int readString (char* content) {
  
  int ret;
  int i = 0;
  
  if (!content)
    error("Bad pointer");
  
  content[0] = 0;

  do {
    
    if ((ret = read(STDIN_FILENO, &content[i], 1) <= 0))
      return 0;
    
  }  while ((content[i++] != '\n') && (i < MAXSTR));

  if (content[i - 1] != '\n') 
    error("Too long content");
  
  content[i - 1] = 0;
  
  return i;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void writeString (char* content, char* buffer) {
  
  const char* space   = " ";
  const char* endLine = "\n";
  
  if (write(STDOUT_FILENO, content, strlen(content)) < 0) error("Error writing");
  if (write(STDOUT_FILENO, space, strlen(space)) < 0)     error("Error writing");
  if (write(STDOUT_FILENO, buffer, strlen(buffer)) < 0)   error("Error writing");
  if (write(STDOUT_FILENO, endLine, strlen(endLine)) < 0) error("Error writing");
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
